/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package traffic;

import com.google.code.eventsonfire.Events;
import com.google.code.eventsonfire.swing.SwingEventHandler;
import entities.FireComplete;
import entities.FireConfig;
import entities.FireDrawGraphics;
import entities.FireStopExecutor;
import entities.Server;
import java.util.ArrayList;
import visualisar.ControladorGraficos;

/**
 *
  * @author Leonardo Lino Nº45340
 * @author Diogo Pires Nº40351
 */
public class GeradorPanel extends javax.swing.JPanel {

    public static final int ESTADO_STOP = 1;
    public static final int ESTADO_RUNNING = 2;
    private int estado = 1;

    Configuracao config;
    ControladorTrafego ct = null;

    /**
     * Creates new form GeradorPanel
     */
    public GeradorPanel() {
        initComponents();
        ct = new ControladorTrafego();
        pedidosVal.setText(Integer.toString(pedidosSlider.getValue()));
        Events.bind(TrafegoRunnable.class, this);
        Events.bind(Configuracao.class, this);
        Events.bind(GestorThreads.class, this);

        ArrayList<Server> servers = ct.getServers();
        

        if (servers != null) {
        Server db = new Server("1", 1);
        db.setNodeName("DBPrimary");
        db.setType(Server.TYPE_DB);
        
        servers.add(db);
        
         comboServer.addItem("AppServer");
        comboServer.addItem("DBPrimary");
       
        
        /*
            for (Server server : servers) {
                comboServer.addItem(server.getNodeName());
            }
        */

        } else {
            comboServer.addItem("no servers found");
            startButton.setEnabled(false);
        }

        if (estado == ESTADO_STOP) {
            stopButton.setEnabled(false);
        }
    }

    @SwingEventHandler
    public void handleEvent(String texto) {
        consola.append("\n" + texto);
    }

    @SwingEventHandler
    public void handleEvent2(FireConfig fire) {
        pedidosSlider.setMaximum(fire.valorMaximo);
    }

    @SwingEventHandler
    public void handleEvent3(FireDrawGraphics fire) {
        ControladorGraficos cg = new ControladorGraficos(this);
        cg.drawPie();
        cg.drawBarChart();
    }

    @SwingEventHandler
    public void handleEvent3(FireComplete fire) {
        estado = ESTADO_STOP;
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
    }

    public void setConfig(Configuracao config) {
        this.config = config;
        ct.setConfig(config);
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this
     * method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        startButton = new javax.swing.JButton();
        stopButton = new javax.swing.JButton();
        pedidosSlider = new javax.swing.JSlider();
        textCurrent = new javax.swing.JLabel();
        pedidosVal = new javax.swing.JLabel();
        comboType = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        segundosField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        comboServer = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        consola = new javax.swing.JTextArea();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        resultadosPainel1 = new visualisar.ResultadosPainel();

        jTextField1.setText("jTextField1");

        startButton.setText("Start");
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });

        stopButton.setText("Stop");
        stopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopButtonActionPerformed(evt);
            }
        });

        pedidosSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                pedidosSliderStateChanged(evt);
            }
        });

        textCurrent.setText("Pedidos por segundo:");

        pedidosVal.setText("0");

        comboType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Constant", "Progressive" }));

        jLabel1.setText("Load Type");

        segundosField.setText("5");

        jLabel2.setText("Load Duration (Sec)");

        jLabel3.setText("Load on Server");

        consola.setColumns(20);
        consola.setRows(5);
        jScrollPane1.setViewportView(consola);

        jTabbedPane1.addTab("Resultado", resultadosPainel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(textCurrent)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pedidosVal)
                        .addGap(101, 101, 101))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(startButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(stopButton))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(38, 38, 38)
                                        .addComponent(segundosField, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel1)
                                            .addComponent(jLabel3))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(comboServer, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(comboType, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                                .addGap(18, 18, 18))
                            .addComponent(pedidosSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(20, 20, 20))))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(textCurrent)
                            .addComponent(pedidosVal))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pedidosSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(comboServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(comboType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(segundosField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(stopButton)
                            .addComponent(startButton))
                        .addGap(0, 102, Short.MAX_VALUE))
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
        ct.setGeradorPainel(this);
        ct.setConfig(config);

        ct.start();
        estado = ESTADO_RUNNING;
        startButton.setEnabled(false);
        stopButton.setEnabled(true);

    }//GEN-LAST:event_startButtonActionPerformed

    private void pedidosSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_pedidosSliderStateChanged
        pedidosVal.setText(Integer.toString(pedidosSlider.getValue()));
    }//GEN-LAST:event_pedidosSliderStateChanged

    private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed
        Events.fire(this, new FireStopExecutor());
        estado = ESTADO_STOP;
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
    }//GEN-LAST:event_stopButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JComboBox comboServer;
    public javax.swing.JComboBox comboType;
    public javax.swing.JTextArea consola;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    public javax.swing.JSlider pedidosSlider;
    private javax.swing.JLabel pedidosVal;
    private visualisar.ResultadosPainel resultadosPainel1;
    public javax.swing.JTextField segundosField;
    private javax.swing.JButton startButton;
    private javax.swing.JButton stopButton;
    private javax.swing.JLabel textCurrent;
    // End of variables declaration//GEN-END:variables
}
